/*
Author Dave Lenz
Creation date: 16 jun 2009
 */
package nl.ypmits.utils.as3.view.interfaces.wheel {
	import flash.display.DisplayObject;
	
	import caurina.transitions.Tweener;
	
	import de.alex_uhlmann.animationpackage.drawing.Arc;
	
	import flash.display.LineScaleMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.geom.ColorTransform;

	public class WheelPart extends Sprite {

		private var id : int;
		private var partradius : Number;
		private var selectable : Boolean;
		public var arc : Arc;
		public var animating : Boolean = false;
		public var showEase : String;
		public var showTime : int;
		public var icon : DisplayObject;

		
		public function WheelPart( _id : int, _partradius : Number, _ease : String = "easeInOutSine", _totalTime : Number = .1 ) {
			id = _id;
			partradius = _partradius;
			showEase = _ease;
			showTime = _totalTime;
			
			addEventListener( Event.ADDED_TO_STAGE, init );
		}

		
		private function init(event : Event) : void {
			removeEventListener( Event.ADDED_TO_STAGE, init );
			
			draw( );
		}

		
		public function clear() : void {
			arc.clear( );
		}

		
		public function select() : void {
			if(!animating) {
				animating = true;
				selectable = true;
				Tweener.addTween( arc.movieclip, { scaleX:1.3, scaleY:1.3, time:.15, transition:"easeInOutCubic", onComplete:stopAnimating } );
				arc.movieclip.transform.colorTransform = new ColorTransform( 1, 1, 1, 1, 0, 0, 0, 0 );
			}
			
			parent.setChildIndex( this, parent.numChildren - 1 );

			var bf : DropShadowFilter = new DropShadowFilter( );
			bf.quality = 3;
			bf.blurX = bf.blurY = 5;
			bf.alpha = .35;
			arc.movieclip.filters = [ bf ];
		}

		
		private function stopAnimating() : void {
			animating = false;
		}

		
		public function deselect() : void {
			if( selectable ) {
				arc.movieclip.scaleX = arc.movieclip.scaleY = 1;
				arc.movieclip.transform.colorTransform = new ColorTransform( 1, 1, 1, 1, 0, 0, 0, 0 );
			}
			arc.movieclip.filters = null;
		}

		
		private function draw() : void {
			var total : Number = (360 / partradius);
			arc = new Arc( 0, 0 );
			arc.setArcType( "PIE" );
			arc.lineStyle( 1, 0x999999, 1, true, LineScaleMode.NONE );
			arc.fillStyle( 0xF1F1F1, 1 );
			arc.setStartValue( 0 );
			arc.setEndValue( 360 / total );
			arc.draw( );
			arc.movieclip.scaleX = arc.movieclip.scaleY = 0;
			arc.movieclip.rotation = (360 / total) * id;
			
			var arc_dupe : Arc = new Arc( 0, 0 );
			arc_dupe.setArcType( "PIE" );
			arc_dupe.lineStyle( 1, 0x999999, 1, true, LineScaleMode.NONE );
			arc_dupe.fillStyle( 0xF1F1F1, 1 );
			arc_dupe.setStartValue( 0 );
			arc_dupe.setEndValue( 360 / total );
			arc_dupe.draw( );
			arc_dupe.movieclip.scaleX = arc.movieclip.scaleY = 0;
			arc_dupe.movieclip.rotation = (360 / total) * id;
			
			if( icon ) {
				addChild( icon );
				icon.mask = arc_dupe.movieclip;
			}
		}

		
		public function show() : void {
			selectable = false;
			Tweener.addTween( arc.movieclip, { scaleX:1, scaleY:1, time:showTime, delay:id * .05, transition:showEase, onComplete:makeSelectable } );
		}

		
		private function makeSelectable() : void {
			selectable = true;
		}
	}
}
